﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Data;
using Team4Mock.Entity;
namespace Team4Mock.UserControl.Organisation
{
    public partial class ViewAll : System.Web.UI.UserControl
    {
        OrganisationEntity oe = new OrganisationEntity();
        SupportEntity s = new SupportEntity();
        DirectorateEntity d = new DirectorateEntity();
        PremiseEntity p = new PremiseEntity();
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        btnDel.Visible = true;
                    }
                    else
                    {
                        btnDel.Visible = false;
                    }
                }
                else
                {
                    btnDel.Visible = false;
                }
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    try
                    {

                        pnct.Visible = true;
                        pngv.Visible = false;
                        pnCreate.Visible = false;

                        int id = int.Parse(Request.QueryString["ID"]);
                        DataTable dt = new DataTable();
                        oe.Organizationid = id;
                        p.Organizationid = id;
                        gvPremise.DataSource = p.GetDataById(p);
                        gvPremise.DataBind();
                        dt = oe.GetDataById(oe);
                        for (int o = 0; o < dt.Rows.Count; o++)
                        {
                            txtOrgName.Text = dt.Rows[0][1].ToString();
                            txtShortDes.Text = dt.Rows[0][2].ToString();
                            idContact.Value = dt.Rows[0][3].ToString();
                            txtAdd1.Text = dt.Rows[0][4].ToString();
                            txtAdd2.Text = dt.Rows[0][5].ToString();
                            txtAdd3.Text = dt.Rows[0][6].ToString();
                            PostCode.Value = dt.Rows[0][7].ToString();
                            Town.Value = dt.Rows[0][8].ToString();
                            County.Value = dt.Rows[0][9].ToString();
                            dlNational.SelectedValue = dt.Rows[0][10].ToString();
                            ckPreferred.Checked = dt.Rows[0][11].ToString().Equals("1") ? true : false;
                            ckExpress.Checked = dt.Rows[0][12].ToString().Equals("1") ? true : false;
                            hdidBus.Value = dt.Rows[0][13].ToString();
                            hdsic.Value = dt.Rows[0][14].ToString();
                            txtFullDes.Text = dt.Rows[0][15].ToString();
                            txtPhone.Text = dt.Rows[0][16].ToString();
                            txtFax.Text = dt.Rows[0][17].ToString();
                            txtWeb.Text = dt.Rows[0][18].ToString();
                            txtCharity.Text = dt.Rows[0][19].ToString();
                            txtCompany.Text = dt.Rows[0][20].ToString();
                            ckIsactive.Checked = dt.Rows[0][21].ToString().Equals("1") ? true : false;
                            if (dt.Rows[0][12].ToString().Equals("1"))
                            {
                                lbdiv.Text = "<li><a href=\"#Details3\" class=\"tab\">Details3</a></li>";
                                GetCheckboxList(oe.GetOEOIPro(), oe.GetOEOIProById(oe), clEOIPro);
                                GetCheckboxList(oe.GetOEOISer(), oe.GetOEOISerById(oe), clEOISer);
                            }

                        }

                        GetCheckboxList(oe.GetAccred(), oe.GetAccById(oe), clAcc);
                        GetCheckboxList(oe.GetSerBenifitCapa(), oe.GetBenefitById(oe), clSerBenCap);
                        GetCheckboxList(oe.GetOrgSpec(), oe.GetSpecById(oe), clOrgSpe);
                        GetCheckboxList(oe.GetSerBarrieCapa(), oe.GetBarriersById(oe), clSerBarCap);
                        GetCheckboxList(oe.GetSerDisaCapa(), oe.GetDisaById(oe), clSerDisCap);
                        GetCheckboxList(oe.GetEthicCapa(), oe.GetEthnicById(oe), clSerEthCap);
                        GetCheckboxList(oe.GetSerPersoCapa(), oe.GetPersoById(oe), clSerPerCirCap);

                        dlNational.DataSource = oe.GetCountry();
                        dlNational.DataBind();

                        s.OrganizationID = id;

                        gvSupport.DataSource = s.GetDataById(s);
                        gvSupport.DataBind();

                        d.OrganizationID = id;
                        gvDirectorate.DataSource = d.GetDataByID(d);
                        gvDirectorate.DataBind();

                        p.PremisesID = id;
                        gvPremise.DataSource = p.GetDataById(p);
                        gvPremise.DataBind();
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
                else
                {
                    pnct.Visible = false;
                    pngv.Visible = true;
                    pnCreate.Visible = false;

                    gvOrg.DataSource = oe.GetdataTrue();
                    gvOrg.DataBind();
                }
            }
        }

        void GetValueCheckBoxList(CheckBoxList cl, string table)
        {
            try
            {
                int id = int.Parse(Request.QueryString["ID"]);
                oe.Organizationid = id;

                string lb = string.Empty;
                for (int i = 0; i < cl.Items.Count; i++)
                {
                    if (cl.Items[i].Selected == true)
                    {
                        lb += cl.Items[i].Value + "=";
                    }
                }
                string[] tmp = lb.Split('=');
                switch (table)
                {
                    case "Acc":
                        {
                            oe.DelAcc(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertAcc(oe);
                            }
                        }
                        break;
                    case "Bar":
                        {
                            oe.DelBarrier(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertBarrier(oe);
                            }
                        }
                        break;
                    case "Ben":
                        {
                            oe.DelBenefit(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertBenefit(oe);
                            }
                        }
                        break;
                    case "Dis":
                        {
                            oe.DelDisa(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertDisa(oe);
                            }
                        }
                        break;
                    case "Eth":
                        {
                            oe.DelEthnic(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertEthnic(oe);
                            }
                        }
                        break;
                    case "Per":
                        {
                            oe.DelPerso(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertPerso(oe);
                            }
                        }
                        break;
                    case "Spe":
                        {
                            oe.DelSpec(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertSpec(oe);
                            }
                        }
                        break;
                    case "OEOISer":
                        {
                            oe.DelOEOISer(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertOEOISer(oe);
                            }
                        }
                        break;
                    case "OEOIPro":
                        {
                            oe.DelOEOIPro(oe);
                            for (int i = 0; i < tmp.Count() - 1; i++)
                            {
                                oe.Refid = int.Parse(tmp[i].ToString());
                                oe.InsertOEOIPro(oe);
                            }
                        }
                        break;
                }
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
            }

        }
        void GetCheckboxList(DataTable dt1, DataTable dt2, CheckBoxList cl)
        {
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                cl.Items.Add(new ListItem(dt1.Rows[i][1].ToString(), dt1.Rows[i][0].ToString()));
            }
            //Get ID
            for (int j = 0; j < cl.Items.Count; j++)
            {
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    if (dt2.Rows[i][0].ToString().Equals(cl.Items[j].Value))
                    {
                        cl.Items[j].Selected = true;
                    }
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int id = int.Parse(Request.QueryString["ID"]);
                if (txtOrgName.Text == "" || txtShortDes.Text == "" || txtAdd1.Text == "" || txtPhone.Text == "" ||
                    hdidBus.Value == "" || hdsic.Value == "" || PostCode.Value == "")
                {
                    Label2.Text = "Input Mandatory fields";
                }
                else
                {
                    try
                    {
                        int contactid = int.Parse(idContact.Value);
                        string idbus = hdidBus.Value;
                        string postcode = PostCode.Value;
                        string sic = hdsic.Value;
                        string town = Town.Value;
                        string county = County.Value;
                        string national = dlNational.SelectedValue;

                        oe.OrganizationName = txtOrgName.Text.Trim();
                        oe.OrganizationShortDescription = txtShortDes.Text;
                        oe.LeadContact = contactid;
                        oe.PreferredOrganization = ckPreferred.Checked ? 1 : 0;
                        oe.ExpressionOfOrganization = ckExpress.Checked ? 1 : 0;
                        oe.TypeOfBusiness = idbus;
                        oe.SICCode = sic;
                        oe.OrFullDescription = txtFullDes.Text;
                        oe.AddressLine1 = txtAdd1.Text;
                        oe.AddressLine2 = txtAdd2.Text;
                        oe.AddressLine3 = txtAdd3.Text;
                        oe.PostCode = postcode;
                        oe.Town = town;
                        oe.County = county;
                        oe.Country = national;
                        oe.PhoneNumber = txtPhone.Text;
                        oe.Fax = txtFax.Text;
                        oe.WebAddress = txtWeb.Text;
                        oe.ChartyNumber = txtCharity.Text;
                        oe.CompanyNumber = txtCompany.Text;
                        oe.Organizationid = id;
                        oe.IsActive = ckIsactive.Checked ? 1 : 0;
                        oe.Update(oe);

                        //update acc
                        GetValueCheckBoxList(clAcc, "Acc");
                        //update bar
                        GetValueCheckBoxList(clSerBarCap, "Bar");
                        //update ben
                        GetValueCheckBoxList(clSerBenCap, "Ben");
                        //update Dis
                        GetValueCheckBoxList(clSerDisCap, "Dis");
                        //update Eth
                        GetValueCheckBoxList(clSerEthCap, "Eth");
                        //update Per
                        GetValueCheckBoxList(clSerPerCirCap, "Per");
                        //update Spe
                        GetValueCheckBoxList(clOrgSpe, "Spe");
                        if (ckExpress.Checked == true)
                        {
                            GetValueCheckBoxList(clEOIPro, "OEOIPro");
                            GetValueCheckBoxList(clEOISer, "OEOISer");
                        }
                        Label2.Text = "Update Success";
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }

                }
            }

        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Organisation.aspx");


        }
        public SortDirection SortDirection
        {
            get
            {
                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }

        protected void gvOrg_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dt = new DataTable();
            if (ViewState["sortkey"] != null)
            {
                string sortkey = ViewState["sortkey"].ToString();
                oe.Key = sortkey;
                if (ckActive.Checked == true)
                {
                    dt = oe.Search(oe);
                }
                else
                {
                    dt = oe.SearchTrue(oe);
                }
            }
            else
            {
                dt = ckActive.Checked ? oe.Getdata() : oe.GetdataTrue();
            }

            string sortExpression = e.SortExpression;
            string direction = string.Empty;
            string sort = "";
            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                direction = " DESC";
                sort = "↑";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                direction = " ASC";
                sort = "↓";
            }
            if (sortExpression == "Name")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name " + sort;
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "Address")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1 " + sort;
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "Postcode")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code " + sort;
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "Contact")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact " + sort;
                gvOrg.Columns[5].HeaderText = "Is Active";
            }
            if (sortExpression == "IsActive")
            {
                gvOrg.Columns[1].HeaderText = "Organisation Name";
                gvOrg.Columns[2].HeaderText = "Head Office Address Line 1";
                gvOrg.Columns[3].HeaderText = "Post Code";
                gvOrg.Columns[4].HeaderText = "Contact";
                gvOrg.Columns[5].HeaderText = "Is Active " + sort;
            }
            dt.DefaultView.Sort = sortExpression + direction;

            gvOrg.DataSource = dt;
            gvOrg.DataBind();

        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            pnct.Visible = false;
            pngv.Visible = false;
            pnCreate.Visible = true;
        }
        protected void btnBack1_Click(object sender, EventArgs e)
        {
            pnct.Visible = false;
            pngv.Visible = true;
            pnCreate.Visible = false;
        }

        private DataTable dt;
        protected void All_Click(object sender, EventArgs e)
        {
            ViewState.Remove("sortkey");
            dt = oe.Getdata();
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                abc.Text = "No Record";
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "ABCDE";
            oe.Key = "ABCDE";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();

                abc.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                abc.Text = "No Record";
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "FGHIJ";
            oe.Key = "FGHIJ";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                abc.Text = "No Record";
            }
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "KMNL";
            oe.Key = "KMNL";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                abc.Text = "No Record";
            }
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "OPQR";
            oe.Key = "OPQR";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                abc.Text = "No Record";
            }
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "STUV";
            oe.Key = "STUV";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                abc.Text = "No Record";
            }
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "WXYZ";
            oe.Key = "WXYZ";
            dt = oe.Search(oe);
            if (dt.Rows.Count > 0)
            {
                gvOrg.DataSource = dt;
                gvOrg.DataBind();
                abc.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                abc.Text = "No Record";
            }
        }

        protected void LAll_Click(object sender, EventArgs e)
        {
            ViewState.Remove("sortdirec");
            d.OrganizationID = int.Parse(Request.QueryString["ID"]);
            dt = d.GetDataByID(d);
            if (dt.Rows.Count > 0)
            {
                gvDirectorate.DataSource = dt;
                gvDirectorate.DataBind();
                lbDirec.Text = "";
            }
            else
            {
                lbDirec.Text = "No Record";
            }
        }

        protected void LAE_Click(object sender, EventArgs e)
        {
            ViewState["sortdirec"] = "ABCDE";
            d.Key = "ABCDE";
            d.OrganizationID = int.Parse(Request.QueryString["ID"]);
            dt = d.Search(d);
            if (dt.Rows.Count > 0)
            {
                gvDirectorate.DataSource = dt;
                gvDirectorate.DataBind();
                lbDirec.Text = "";
            }
            else
            {
                ViewState.Remove("sortdirec");
                lbDirec.Text = "No Record";
            }
        }

        protected void LFJ_Click(object sender, EventArgs e)
        {
            ViewState["sortdirec"] = "FGHIJ";
            d.Key = "FGHIJ";
            d.OrganizationID = int.Parse(Request.QueryString["ID"]);
            dt = d.Search(d);
            if (dt.Rows.Count > 0)
            {
                gvDirectorate.DataSource = dt;
                gvDirectorate.DataBind();
                lbDirec.Text = "";
            }
            else
            {
                ViewState.Remove("sortdirec");
                lbDirec.Text = "No Record";
            }
        }

        protected void LKL_Click(object sender, EventArgs e)
        {
            ViewState["sortdirec"] = "KMNL";
            d.Key = "KMNL";
            d.OrganizationID = int.Parse(Request.QueryString["ID"]);
            dt = d.Search(d);
            if (dt.Rows.Count > 0)
            {
                gvDirectorate.DataSource = dt;
                gvDirectorate.DataBind();
                lbDirec.Text = "";
            }
            else
            {
                ViewState.Remove("sortdirec");
                lbDirec.Text = "No Record";
            }
        }

        protected void LOR_Click(object sender, EventArgs e)
        {
            ViewState["sortdirec"] = "OPQR";
            d.Key = "OPQR";
            d.OrganizationID = int.Parse(Request.QueryString["ID"]);
            dt = d.Search(d);
            if (dt.Rows.Count > 0)
            {
                gvDirectorate.DataSource = dt;
                gvDirectorate.DataBind();
                lbDirec.Text = "";
            }
            else
            {
                ViewState.Remove("sortdirec");
                lbDirec.Text = "No Record";
            }
        }

        protected void LSV_Click(object sender, EventArgs e)
        {
            ViewState["sortdirec"] = "STUV";
            d.Key = "STUV";
            d.OrganizationID = int.Parse(Request.QueryString["ID"]);
            dt = d.Search(d);
            if (dt.Rows.Count > 0)
            {
                gvDirectorate.DataSource = dt;
                gvDirectorate.DataBind();
                lbDirec.Text = "";
            }
            else
            {
                ViewState.Remove("sortdirec");
                lbDirec.Text = "No Record";
            }
        }

        protected void LWZ_Click(object sender, EventArgs e)
        {
            ViewState["sortdirec"] = "WXYZ";
            d.Key = "WXYZ";
            d.OrganizationID = int.Parse(Request.QueryString["ID"]);
            dt = d.Search(d);
            if (dt.Rows.Count > 0)
            {
                gvDirectorate.DataSource = dt;
                gvDirectorate.DataBind();
                lbDirec.Text = "";
            }
            else
            {
                ViewState.Remove("sortdirec");
                lbDirec.Text = "No Record";
            }
        }

        protected void ckActive_CheckedChanged(object sender, EventArgs e)
        {
            if (ViewState["sortkey"] != null)
            {
                string sort = ViewState["sortkey"].ToString();
                oe.Key = sort;
                if (ckActive.Checked == true)
                {
                    gvOrg.DataSource = oe.Search(oe);
                    gvOrg.DataBind();
                }
                else
                {
                    gvOrg.DataSource = oe.SearchTrue(oe);
                    gvOrg.DataBind();
                }
            }
            else
            {
                if (ckActive.Checked == true)
                {
                    gvOrg.DataSource = oe.Getdata();
                    gvOrg.DataBind();
                }
                else
                {

                    gvOrg.DataSource = oe.GetdataTrue();
                    gvOrg.DataBind();
                }
            }

        }

        protected void btnCreateSup_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Support.aspx?Org=" + Request.QueryString["ID"]);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Directorate.aspx?Org=" + Request.QueryString["ID"]);
        }

        protected void gvOrg_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrg.PageIndex = e.NewPageIndex;
            gvOrg.DataSource = oe.GetdataTrue();
            gvOrg.DataBind();
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(Request.QueryString["ID"]);
                oe.Organizationid = id;
                oe.Del(oe);
                Response.Redirect("/FormView/Organisation.aspx");
            }
            catch
            {
                tb.Text = "Data is being referenced by another table";
            }
        }

        protected void gvDirectorate_Sorting(object sender, GridViewSortEventArgs e)
        {

            DataTable dt = new DataTable();
            int id = int.Parse(Request.QueryString["ID"]);
            if (ViewState["sortdirec"] != null)
            {
                string sortkey = ViewState["sortdirec"].ToString();
                d.Key = sortkey;
                d.OrganizationID = id;
                if (CheckBox1.Checked == true)
                {
                    dt = d.Search(d);
                }
                else
                {
                    dt = d.SearchTrue(d);
                }
            }
            else
            {
                d.OrganizationID = id;
                dt = CheckBox1.Checked ? d.GetDataByID(d) : d.GetdataTrue(d);
            }

            string sortExpression = e.SortExpression;
            string direction = string.Empty;
            string sort = "";
            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                direction = " DESC";
                sort = "↑";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                direction = " ASC";
                sort = "↓";
            }
            if (sortExpression == "DirectorateName")
            {
                gvDirectorate.Columns[1].HeaderText = "Directorate Name " + sort;
                gvDirectorate.Columns[2].HeaderText = "Head Office Address Line 1";
                gvDirectorate.Columns[3].HeaderText = "Post Code";
                gvDirectorate.Columns[4].HeaderText = "Contact";
            }
            if (sortExpression == "AddressLine1")
            {
                gvDirectorate.Columns[1].HeaderText = "Directorate Name";
                gvDirectorate.Columns[2].HeaderText = "Head Office Address Line 1 " + sort;
                gvDirectorate.Columns[3].HeaderText = "Post Code";
                gvDirectorate.Columns[4].HeaderText = "Contact";
            }
            if (sortExpression == "Postcode")
            {
                gvDirectorate.Columns[1].HeaderText = "Directorate Name";
                gvDirectorate.Columns[2].HeaderText = "Head Office Address Line 1";
                gvDirectorate.Columns[3].HeaderText = "Post Code " + sort;
                gvDirectorate.Columns[4].HeaderText = "Contact";
            }
            if (sortExpression == "Contact")
            {
                gvDirectorate.Columns[1].HeaderText = "Directorate Name";
                gvDirectorate.Columns[2].HeaderText = "Head Office Address Line 1";
                gvDirectorate.Columns[3].HeaderText = "Post Code";
                gvDirectorate.Columns[4].HeaderText = "Contact " + sort;
            }
            dt.DefaultView.Sort = sortExpression + direction;

            gvDirectorate.DataSource = dt;
            gvDirectorate.DataBind();
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (ViewState["sortdirec"] != null)
            {
                int id = int.Parse(Request.QueryString["ID"]);
                string sort = ViewState["sortdirec"].ToString();
                d.Key = sort;
                d.OrganizationID = id;
                if (CheckBox1.Checked == true)
                {
                    gvDirectorate.DataSource = d.Search(d);
                    gvDirectorate.DataBind();
                }
                else
                {
                    gvDirectorate.DataSource = d.SearchTrue(d);
                    gvDirectorate.DataBind();
                }
            }
            else
            {
                int id = int.Parse(Request.QueryString["ID"]);
                if (CheckBox1.Checked == true)
                {
                    gvDirectorate.DataSource = d.GetDataByID(d);
                    gvDirectorate.DataBind();
                }
                else
                {

                    gvDirectorate.DataSource = d.GetdataTrue(d);
                    gvDirectorate.DataBind();
                }
            }
        }
    }
}